<?php
/**
 * Description of QuestionFacade
 *
 * @author Alexey
 */
class QuestionFacade {

    public static function isAuthor(Question $question)
    {
        $userId = Yii::app()->controller->getUserIdFromSession();
        return ($question->author_id == $userId);
    }

    public static function loadModel($id)
    {
        $model = Question::model()->findByPk( ( int ) $id );
        if ( $model === null )
            throw new CHttpException( 404, 'The requested page does not exist.' );
        return $model;
    }

    public static function getAuthorUser(Question $question)
    {
        $user = UserFacade::getUserById($question->author_id);
        return $user;
    }

    public static function saveModel(Question $model)
    {
        $isNewVideo = $model->isNewRecord;
        $success = true;
        if ($isNewVideo)
        {
            $userId = Yii::app()->controller->getUserIdFromSession();
            $model->author_id = $userId;
            $commentFlow = new CommentFlow;
            $success &= $commentFlow->save();
            $model->comment_flow_id = $commentFlow->comment_flow_id;
        }
        $success &= $model->save();
        return $success;

    }

    public static function deleteModel($id)
    {
        $question = QuestionFacade::loadModel( $id );
        $commentFlowId = $question->comment_flow_id;
        $question->comment_flow_id = NULL;
        $question->save();
        CommentFacade::deleteCommentFlowModel($commentFlowId);
        $question->delete();
    }

    private static function getQuestionDBCriteriaByUserId($userId)
    {
        if (empty($userId))
        {
            throw new CException("UserId can not be null.");
        }
        $criteria = new CDbCriteria();
        $criteria->condition = 'author_id = '. $userId;
        $criteria->order = "publish_date DESC";
        return $criteria;
    }

    public static function getDataProviderByUser($user)
    {
        $dataProvider = new CActiveDataProvider( 'Question', array(
            'criteria'=>QuestionFacade::getQuestionDBCriteriaByUserId( $user->user_id )
        ));

        return $dataProvider;

    }

    public static function getLatesUserQuestion($userId, $count = 3)
    {
        $criteria = QuestionFacade::getQuestionDBCriteriaByUserId( $userId );
        $criteria->limit = $count;
        $question = Question::model()->findAll($criteria);
        return $question;

    }

    public static function getQuestionCountByUser($userId)
    {
        $criteria = QuestionFacade::getQuestionDBCriteriaByUserId( $userId );
        return Question::model()->count($criteria);
    }

    public static function getDataProvider()
    {
        $criteria = new CDbCriteria();
        $criteria->order = "publish_date DESC";
        $dataProvider = new CActiveDataProvider( 'Question', array(
            'criteria'=> $criteria
        ));

        return $dataProvider;
    }

    public static function getLatesQuestion($count)
    {
        $criteria = new CDbCriteria();
        $criteria->order = "publish_date DESC";
        $criteria->limit = $count;
        $question = Question::model()->findAll($criteria);
        return $question;
    }

    public static function getQuestionCount()
    {
        return Question::model()->count();
    }
}
?>
